#include "../include/test_framework.h"

// 命令解析线程
void* command_parser_thread(void *arg) {
    (void)arg; // 抑制未使用参数警告
    char input_buffer[MAX_CMD_LEN];
    test_command_t cmd;
    
    pthread_mutex_lock(&g_test_framework.status_mutex);
    g_test_framework.parser_status = THREAD_RUNNING;
    pthread_mutex_unlock(&g_test_framework.status_mutex);
    
    log_message("INFO", "命令解析线程已启动");
    
    while (g_test_framework.running) {
        // 检查是否有输入可读
        if (!check_stdin_ready()) {
            continue;
        }
        
        // 提示符
        printf("AI_GLASS> ");
        fflush(stdout);
        
        // 读取用户输入
        if (fgets(input_buffer, sizeof(input_buffer), stdin) == NULL) {
            if (g_test_framework.running) {
                log_message("ERROR", "读取输入失败");
            }
            break;
        }
        
        // 解析命令
        if (parse_command_parameters(input_buffer, &cmd) == 0) {
            // 处理退出命令
            if (cmd.type == CMD_EXIT) {
                log_message("INFO", "收到退出命令");
                g_test_framework.running = 0;
                break;
            }
            
            // 将命令推入队列
            if (cmd_queue_push(&g_test_framework.cmd_queue, &cmd) == 0) {
                log_message("DEBUG", "命令已加入队列: %s", cmd.cmd_string);
            } else {
                log_message("WARN", "命令队列已满，丢弃命令: %s", cmd.cmd_string);
            }
        } else {
            printf("命令解析失败，请检查输入格式\n");
        }
    }
    
    pthread_mutex_lock(&g_test_framework.status_mutex);
    g_test_framework.parser_status = THREAD_STOPPED;
    pthread_mutex_unlock(&g_test_framework.status_mutex);
    
    log_message("INFO", "命令解析线程已停止");
    return NULL;
}

// 命令处理线程
void* command_processor_thread(void *arg) {
    (void)arg; // 抑制未使用参数警告
    test_command_t cmd;
    test_result_t result;
    
    pthread_mutex_lock(&g_test_framework.status_mutex);
    g_test_framework.processor_status = THREAD_RUNNING;
    pthread_mutex_unlock(&g_test_framework.status_mutex);
    
    log_message("INFO", "命令处理线程已启动");
    
    while (g_test_framework.running) {
        // 从队列中获取命令
        if (cmd_queue_pop(&g_test_framework.cmd_queue, &cmd) == 0) {
            pthread_mutex_lock(&g_test_framework.status_mutex);
            g_test_framework.processor_status = THREAD_BUSY;
            pthread_mutex_unlock(&g_test_framework.status_mutex);
            
            log_message("INFO", "开始处理命令: %s", cmd.cmd_string);
            
            // 处理命令
            result = process_command(&cmd);
            
            // 输出结果
            printf("命令执行结果: %s -> %s\n", 
                   cmd.cmd_string, get_result_string(result));
            
            pthread_mutex_lock(&g_test_framework.status_mutex);
            g_test_framework.processor_status = THREAD_RUNNING;
            pthread_mutex_unlock(&g_test_framework.status_mutex);
            
            log_message("INFO", "命令处理完成: %s [%s]", 
                       cmd.cmd_string, get_result_string(result));
        }
    }
    
    pthread_mutex_lock(&g_test_framework.status_mutex);
    g_test_framework.processor_status = THREAD_STOPPED;
    pthread_mutex_unlock(&g_test_framework.status_mutex);
    
    log_message("INFO", "命令处理线程已停止");
    return NULL;
}

// 监控线程
void* monitor_thread(void *arg) {
    (void)arg; // 抑制未使用参数警告
    int monitor_interval = 10; // 10秒监控间隔
    
    pthread_mutex_lock(&g_test_framework.status_mutex);
    g_test_framework.monitor_status = THREAD_RUNNING;
    pthread_mutex_unlock(&g_test_framework.status_mutex);
    
    log_message("INFO", "监控线程已启动");
    
    while (g_test_framework.running) {
        sleep(monitor_interval);
        
        if (!g_test_framework.running) break;
        
        // 监控队列状态
        pthread_mutex_lock(&g_test_framework.cmd_queue.mutex);
        int queue_count = g_test_framework.cmd_queue.count;
        pthread_mutex_unlock(&g_test_framework.cmd_queue.mutex);
        
        // 监控线程状态
        pthread_mutex_lock(&g_test_framework.status_mutex);
        thread_status_t parser_status = g_test_framework.parser_status;
        thread_status_t processor_status = g_test_framework.processor_status;
        pthread_mutex_unlock(&g_test_framework.status_mutex);
        
        log_message("MONITOR", "队列命令数: %d, 解析线程: %s, 处理线程: %s",
                   queue_count, 
                   get_thread_status_string(parser_status),
                   get_thread_status_string(processor_status));
    }
    
    pthread_mutex_lock(&g_test_framework.status_mutex);
    g_test_framework.monitor_status = THREAD_STOPPED;
    pthread_mutex_unlock(&g_test_framework.status_mutex);
    
    log_message("INFO", "监控线程已停止");
    return NULL;
}

// 信号处理函数
void signal_handler(int sig) {
    switch (sig) {
        case SIGINT:
        case SIGTERM:
            printf("\n收到退出信号，正在关闭测试框架...\n");
            g_test_framework.running = 0;
            break;
        default:
            break;
    }
}

// 设置信号处理
void setup_signal_handlers(void) {
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGPIPE, SIG_IGN); // 忽略SIGPIPE信号
}